package com.http;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.URL;

/**
 * HttpURLConnection实现get post请求
 * 
 * @author zincredible
 * @date 2019/04/12 13:28:40
 */
public class JavaHttpUtil {

	/**
	 * post請求
	 * 
	 * @param urlPath
	 * @param json
	 * @return
	 * @date 2020/04/15 14:15:59
	 * @author zincredible
	 */
	public static String post(String urlPath, String json) {
		HttpURLConnection httpURLConnection = null;
		InputStream in = null;
		OutputStreamWriter os = null;
		String content = null;
		try {
			URL url = new URL(urlPath);
			httpURLConnection = (HttpURLConnection) url.openConnection();
			httpURLConnection.setRequestMethod("POST");
			httpURLConnection.setRequestProperty("Charset", "UTF-8");
			// 设置接收数据的格式
			httpURLConnection.setRequestProperty("Accept", "application/json");
			// 设置发送数据的格式
			httpURLConnection.setRequestProperty("Content-Type", "application/json");
			httpURLConnection.setDoOutput(true);
			httpURLConnection.setDoInput(true);
			os = new OutputStreamWriter(httpURLConnection.getOutputStream());
			os.append(json);
			os.flush();
			in = httpURLConnection.getInputStream();
			byte buffer[] = new byte[1024];
			int len = 0;
			StringBuilder sb = new StringBuilder();
			while ((len = in.read(buffer)) > 0)
				if (len > 0)
					sb.append(new String(buffer, 0, len));
			content = sb.toString();
		} catch (Exception e) {
			log(e.toString());
		} finally {
			try {
				if (in != null)
					in.close();
				if (os != null)
					os.close();
				if (httpURLConnection != null)
					httpURLConnection.disconnect();
			} catch (IOException e) {
				log(e.toString());
			}
		}
		return content;
	}

	/**
	 * get请求
	 * 
	 * @param urlPath
	 * @return
	 */
	public static String get(String urlPath) {
		HttpURLConnection connection = null;
		BufferedReader reader = null;
		String result = "";
		try {
			URL url = new URL(urlPath);
			connection = (HttpURLConnection) url.openConnection();
			connection.setRequestMethod("GET");
			InputStream in = connection.getInputStream();
			reader = new BufferedReader(new InputStreamReader(in, "utf-8"));
			StringBuilder sb = new StringBuilder();
			String line;
			while ((line = reader.readLine()) != null)
				sb.append(line);
			result = sb.toString();
		} catch (Exception e) {
			log(e.toString());
		} finally {
			try {
				if (reader != null)
					reader.close();
				if (connection != null)
					connection.disconnect();
			} catch (IOException e) {
				log(e.toString());
			}
		}
		return result;
	}

	public static boolean ping(String host) {
		try {
			InetAddress inetAddress = InetAddress.getByName(host);
			boolean reachable = inetAddress.isReachable(5 * 1000);
			if (reachable)
				return true;
			return false;
		} catch (IOException e) {
			log(e.toString());
		}
		return false;
	}

	public static void log(String log) {
		System.out.println(log);
	}
}
